package com.xzz.string;

/**
 * @author: hhz
 * @create: 2022-01-10 13:54
 * 415. 字符串相加
 * 给定两个字符串形式的非负整数num1 和num2，计算它们的和并同样以字符串形式返回。
 *
 * 你不能使用任何內建的用于处理大整数的库（比如 BigInteger），也不能直接将输入的字符串转换为整数形式。
 *
 **/
public class AddStrings {
    public static void main(String[] args) {
        System.out.println(addStrings("11","123"));
        System.out.println(addStrings("456","77"));
    }
    public static String addStrings(String num1, String num2) {
        StringBuilder sb1 = new StringBuilder(num1);
        StringBuilder sb2 = new StringBuilder(num2);
        StringBuilder sb = new StringBuilder();
        int len1 = sb1.length();
        int len2 = sb2.length();
        int len = Math.max(len1, len2);
        int pre = 0;
        while(len1<len){
            sb1.insert(0,"0");
            len1++;
        }
        while(len2<len){
            sb2.insert(0,"0");
            len2++;
        }
        for (int i = len-1; i >=0 ; i--) {
            int cur = sb1.charAt(i)-'0'+sb2.charAt(i)-'0'+pre;
            int ex = cur%10;
            pre = cur/10;
            sb.append(ex);
        }
        if(pre!=0){
            sb.append(pre);
        }
        return sb.reverse().toString();

    }
}
